﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PTTPOS_Solution.DAL
{
    public static class Account_DA
    {
        public static string staff_ID, staffName, companyName, companyLogo, Storelink, printbill;
        public static List<string> staffRule_ID;
        public static bool login(string username, string password)
        {
            bool chk = false;
            MySqlCommand command = new MySqlCommand(@"select tblstaffs.id as id, tblstaffs.fullName from tblaccounts,tblstaffs,tblruleaccounts, tblrules where tblstaffs.id=tblaccounts.id_staff and tblstaffs.id=tblruleaccounts.id_staff and tblrules.id=tblruleaccounts.id_rule and username=@username and password=@password and tblrules.id=3
", Connection_PTTPOS.GetConnection());
            command.Parameters.AddWithValue("@username", username);
            command.Parameters.AddWithValue("@password", password);
            command.Connection.Open();
            MySqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                chk = true;
                staff_ID = reader["id"].ToString();
                staffName = reader["fullName"].ToString();
                getListStaffRuleID(staff_ID);
                getCompanyName();
            }
            command.Connection.Close();
            return chk;
        }

        public static void getCompanyName()
        {
            MySqlCommand command = new MySqlCommand("select * from tblcompanies", Connection_PTTPOS.GetConnection());
            command.Connection.Open();
            MySqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                companyName = reader["Companyname"].ToString();
                companyLogo = reader["Logo"].ToString();
                Storelink = reader["Storelink"].ToString();
                printbill = reader["printbill"].ToString();
            }
            command.Connection.Close();
        }
        public static void getListStaffRuleID(string staff_ID)
        {
            MySqlCommand command = new MySqlCommand("select * from tblstaffs,tblruleaccounts where tblstaffs.id=tblruleaccounts.id_staff and tblstaffs.id=@id", Connection_PTTPOS.GetConnection());
            command.Parameters.AddWithValue("@id", staff_ID);
            command.Connection.Open();
            MySqlDataReader reader = command.ExecuteReader();
            staffRule_ID = new List<string>();
            while (reader.Read())
            {
                staffRule_ID.Add(reader["id_rule"].ToString());
            }
            command.Connection.Close();
        }
    }
}
